<?php
declare(strict_types=1);

namespace app\common\listener;

use think\Container;
use think\facade\Log;
use think\swoole\Websocket;

class WsClose
{
    protected Websocket $websocket;
    protected Container $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
        $this->websocket = $container->make(Websocket::class);
    }

    public function handle($event): void
    {
        $fd = $event->fd; // 获取关闭的连接标识符
        echo "WebSocket连接关闭: fd={$fd}\n";

        // 清除 CompanyScreen 中的定时器
        $companyScreen = $this->container->make(CompanyScreen::class);
        $companyScreen->delTickId($fd);

        // 清除 GroupScreen 中的定时器
        $groupScreen = $this->container->make(GroupScreen::class);
        $groupScreen->clearTimers($fd);

        // 可选：记录日志
        Log::info("WebSocket连接关闭: fd={$fd}，已清理相关定时器");
    }
}